# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=ldns
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.7.0
pkgrel=5
pkgdesc="Fast DNS library supporting recent RFCs (mingw-w64)"
arch=('any')
url='https://www.nlnetlabs.nl/projects/ldns/'
license=('custom:BSD')
makedepends=("${MINGW_PACKAGE_PREFIX}-doxygen"
             "${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-python3")
depends=("${MINGW_PACKAGE_PREFIX}-openssl")
optdepends=("${MINGW_PACKAGE_PREFIX}-dnssec-anchors")
options=(strip staticlibs)
source=("https://www.nlnetlabs.nl/downloads/${_realname}/${_realname}-${pkgver}.tar.gz"{,.asc}
        ldns-1.6.17-relocate.patch)
sha256sums=('c19f5b1b4fb374cfe34f4845ea11b1e0551ddc67803bd6ddd5d2a20f0997a6cc'
            'SKIP'
            '29728dac1131c0605e2742d40c7d6165d3c90558bab1b699b08408d38b669a33')
validpgpkeys=('DC34EE5DB2417BCC151E5100E5F8F8212F77A498')

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/ldns-1.6.17-relocate.patch

  WANT_AUTOMAKE=latest autoreconf -fiv
}

build() {
  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}"
  cp -rf ${_realname}-${pkgver} build-${MINGW_CHOST}
  #mkdir -p "${srcdir}/build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"

  # for asctime_r(): https://github.com/msys2/MINGW-packages/issues/6714
  CFLAGS+=" -D_POSIX_C_SOURCE"
  CXXFLAGS+=" -D_POSIX_C_SOURCE"

  PYTHON=${MINGW_PREFIX}/bin/python3 \
  ./configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --enable-shared \
    --enable-static \
    --sysconfdir=${MINGW_PREFIX}/etc \
    --localstatedir=${MINGW_PREFIX}/var \
    --with-trust-anchor=${MINGW_PREFIX}/etc/trusted-key.key \
    --with-drill \
    --with-examples \
    --with-ssl=${MINGW_PREFIX} \
    --without-pyldns \
    --without-pyldnsx \
    --disable-dane-verify

  make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make -j1 DESTDIR="${pkgdir}" install
  install -Dm644 ${srcdir}/${_realname}-${pkgver}/LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
